package com.jy.appmarket.download;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

/**
 * 类说明：
 * 
 * @author Baker.li
 * @date 2015年7月7日
 * @version 1.0
 */

public class NetStateReceiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		if (context == null || intent == null || intent.getAction() == null) {
			return;
		}
		if (intent.getAction().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
			ConnectivityManager cm = (ConnectivityManager) context
					.getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo ni = null;
			try {
				ni = cm.getActiveNetworkInfo();
			} catch (Exception e) {
			}
			if (ni == null) {
				return;
			}
			if (ni.getType() == ConnectivityManager.TYPE_WIFI
					&& ni.getState() == NetworkInfo.State.CONNECTED) {
				Intent it = new Intent(context, DownloadService.class);
				it.setAction(DownloadService.ACTION_RESUME_DOWNLOAD);
				context.startService(it);
			}

		}

	}
}
